package com.tzy.order.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzy.common.constant.WebResult;
import com.tzy.common.constant.WebResultMsgCode;
import com.tzy.common.controller.BaseController;
import com.tzy.common.entity.Orders;
import com.tzy.common.entity.Shop;
import com.tzy.common.entity.ShoppingCart;
import com.tzy.order.config.UserList;
import com.tzy.order.service.OrdersService;
import com.tzy.order.util.GetNumberUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 刘舒
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/orders")
public class OrdersController extends BaseController {
    @Resource
    private OrdersService ordersService;

    /**
     * 分页查询
     * @param pageNum 从第几页开始查
     * @param size 每页查几条
     * @return
     */
    @GetMapping("/orderListByPage")
    public WebResult fenYe(@RequestParam (defaultValue = "0") Integer pageNum,

                           @RequestParam (defaultValue = "5") Integer size,
                           @RequestParam String shopName){
        Page<Orders> ordersPage = ordersService.orderListByPage(pageNum,5,shopName);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("ordersPage",ordersPage);
    }




    /**
     * 根据userid查询订单信息
     * @return
     */
    @GetMapping("/orderlistById")
    public WebResult list(HttpServletRequest request){
        int userId=getUserId(request);
        List<Orders> list = ordersService.getListById(userId);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("list",list);
    }


    /**
     * 根据订单id连表查询订单信息
     * @param orderId
     * @return
     */
    @GetMapping("/order")
    public WebResult order(@RequestParam int orderId){
        Orders order = ordersService.getOrder(orderId);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("order",order);
    }



    /**
     * 修改订单信息
     * @param orders
     * @return
     */
    @PostMapping("/update")
    public WebResult update(@RequestBody Orders orders){
        int jg=ordersService.update(orders);
        if(jg>0){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("update", true);
        }else {
            return new WebResult(WebResultMsgCode.SUCCESS).putData("update", false);
        }
    }

    /**
     * 添加订单
     * @return
     */
    @PostMapping("/insert")
    public WebResult insert(HttpServletRequest request,@RequestBody Orders orders){
        int userId=getUserId(request);
        orders.setUserId(userId);
        String jg=ordersService.insert(orders);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("insert", jg);
    }

    @GetMapping("/sel")
    public WebResult sel(@RequestParam(value = "orderId",required = true) String orderId){
       return new WebResult(WebResultMsgCode.SUCCESS).putData("sel",ordersService.selorder(orderId));
    }

    /**
     * 根据id删除订单信息
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public WebResult removeById(@RequestParam int id){
//        int orderId = getOrderId(request);
       Orders orders=new Orders();
       orders.setId(id);
       orders.setStatus(0);
       boolean updateById=ordersService.updateById(orders);
       return new WebResult(WebResultMsgCode.SUCCESS,updateById);
    }
    /**
     * 批量删除订单信息
     * @param ids
     * @return
     */
    @GetMapping("/remove")
    public WebResult remove(String[] ids){
//        String[] ids= ids.split(",");
        int jg =ordersService.deleteOrderByIds(ids);
        if(jg>0){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("delete", true);
        }else {
            return new WebResult(WebResultMsgCode.SUCCESS).putData("delete", false);
        }

    }
}
